<?php

namespace App\Http\Controllers\Managea;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\CategoryRepository;
use App\Traits\BaseFun;

class CategoryController extends Controller
{
	public function __construct(CategoryRepository $CategoryRepository)
    {
        $this->CategoryRepository=$CategoryRepository;
    }
    public function Category(Request $request)
    {   
        $where=[];
        if ($request->search) {
            $where=['search'=>$request->search];
        }
        $res = $this->CategoryRepository->pageList($where);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function CategoryList(Request $request)
    {   

        $res = $this->CategoryRepository->list();
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function CategoryAdd(Request $request)
    {   
        $res = $this->CategoryRepository->create($request->all());
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '添加失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function CategoryEdit(Request $request,$id)
    {
        $res=$this->CategoryRepository->info(['id'=>$id]);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function CategoryUpdate(Request $request)
    {
        $res=$this->CategoryRepository->update($request->all());
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '修改成功!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function CategoryDel(Request $request,$id)
    {
        $res=$this->CategoryRepository->delete($id);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '删除失败或没有此数据!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }

   

}
